package net.linlan.stage.security;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/**
 *
 * Filename:JasyptUtils.java
 * Desc:Jasypt安全框架加密类工具包
 * 此文件仅保留在本工程中，其他stage工程内不需要保留此文件
 * 此文件由运维发布人员统一配置和管理
 *
 * @author Linlan
 * CreateTime:9/23/21 9:14 PM
 *
 * @version 1.0
 * @since 1.0
 *
 */
public class JasyptUtils {

    /**
     * Jasypt生成加密结果
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待加密值
     * @return  加密后的密码
     */
    public static String encryptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.encrypt(value);
        return result;
    }

    /**
     * 解密
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待解密密文
     * @return  解密后的密码
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.decrypt(value);
        return result;
    }

    /**
     * @param password salt
     * @return  加密对象
     */
    public static SimpleStringPBEConfig cryptOr(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName(null);
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    /**
     * @param args  参数
     */
    public static void main(String[] args) {
        // 首先进行密码加密
//        System.out.println(encryptPwd("Linlan2023!GovStage@Dev", "jdbc:oracle:thin:@124.225.223.110:1521:orcl"));
        // 然后进行密码解密
//        System.out.println(decyptPwd("Linlan2023!GovStage@Dev", "SWpjtikNOJJTUMeaxK9I2qs8RaKzLU0Dy1Pe+YF9rr+d3WP6jcpTQVgQoPuxYDzNr6k5ETr+3mo="));

    }

}
